/*
  * desc  :首页 （排行榜界面 当前选择人物界面）
  * author:<zhouzhiwei>
  * @param:
  * date  : 2019/1/28
*/

var GameData = require('../module/GameData')();

cc.Class({
    extends: cc.Component,

    properties: {
        rankItem: cc.Prefab,
        libao_time: cc.Label,
        rank_list: cc.ScrollView,
        hero: cc.Sprite,
        hpNum: cc.Label,
        attackNum: cc.Label,
        heroAtlas: cc.SpriteAtlas,
        ownRank: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        GameData.getlocalData("gameData");
        GameData.game_start = this;
        this.content = this.rank_list.content;
        this.initRank();
        this.initPeople();

    },


    /*
      * desc  :实例化排行榜
      * author:<zhouzhiwei>
      * @param:
      * date  : 2019/1/26
    */
    initRank() {
        var data1 = {
            response: "{\"data\":[{\"rank\":1,\"score\":102,\"format_score\":\"102\",\"ts\":1548212683,\"extra_data\":\"\",\"player\":{\"name\":\"\\u5f20\\u9e4f\\u98de\",\"photo\":\"https:\\/\\/platform-lookaside.fbsbx.com\\/platform\\/instantgames\\/profile_pic.jpg?igpid=1767543000016557&height=256&width=256&ext=1551098550&hash=AeQyzVVF1FTP5dPi\",\"player_id\":\"1767543000016557\"}},{\"rank\":2,\"score\":95,\"format_score\":\"95\",\"ts\":1548221856,\"extra_data\":\"\",\"player\":{\"name\":\"\\u674e\\u537f\",\"photo\":\"https:\\/\\/platform-lookaside.fbsbx.com\\/platform\\/instantgames\\/profile_pic.jpg?igpid=2226043777445711&height=256&width=256&ext=1551098550&hash=AeTCVdFvPpLJbRrl\",\"player_id\":\"2226043777445711\"}},{\"rank\":3,\"score\":51,\"format_score\":\"51\",\"ts\":1548399917,\"extra_data\":\"\",\"player\":{\"name\":\"Mi Peng Hui\",\"photo\":\"https:\\/\\/platform-lookaside.fbsbx.com\\/platform\\/instantgames\\/profile_pic.jpg?igpid=2205894762796490&height=256&width=256&ext=1551098550&hash=AeTl_sM3XuU9zlQ-\",\"player_id\":\"2205894762796490\"}},{\"rank\":4,\"score\":50,\"format_score\":\"50\",\"ts\":1548401152,\"extra_data\":\"\",\"player\":{\"name\":\"Maples Lc\",\"photo\":\"https:\\/\\/platform-lookaside.fbsbx.com\\/platform\\/instantgames\\/profile_pic.jpg?igpid=2106271216086494&height=256&width=256&ext=1551098550&hash=AeRfwEgrRccz8KOH\",\"player_id\":\"2106271216086494\"}},{\"rank\":5,\"score\":33,\"format_score\":\"33\",\"ts\":1548385930,\"extra_data\":\"\",\"player\":{\"name\":\"\\u8bb8\\u8bed\\u8f69\",\"photo\":\"https:\\/\\/platform-lookaside.fbsbx.com\\/platform\\/instantgames\\/profile_pic.jpg?igpid=1741393579272920&height=256&width=256&ext=1551098550&hash=AeS9CVVWvm40hiEa\",\"player_id\":\"1741393579272920\"}},{\"rank\":6,\"score\":30,\"format_score\":\"30\",\"ts\":1548422491,\"extra_data\":\"\",\"player\":{\"name\":\"\\u5b59\\u78ca\",\"photo\":\"https:\\/\\/platform-lookaside.fbsbx.com\\/platform\\/instantgames\\/profile_pic.jpg?igpid=2607309642629541&height=256&width=256&ext=1551098550&hash=AeRPgJG2E1gX8iUH\",\"player_id\":\"2607309642629541\"}}]}"
        }
        var data = JSON.parse(data1.response).data;
        for (var a = 0; a < data.length; a++) {
            var item = cc.instantiate(this.rankItem);
            /*特殊排名需要额外处理*/
            if (a == 0) {

            }
            item.getComponent('rankitem').init(data[a]);
            this.content.addChild(item);
        }
        this.ownRank.getComponent('rankitem').init(data[0]);


    },
    initPeople() {
        var id;
        if (GameData.gameData.selectId) {
            id = GameData.gameData.selectId;
        } else {
            id = 1;
        }
        this.hero.node.getComponent(cc.Sprite).spriteFrame = this.heroAtlas.getSpriteFrame("people" + id);
        this.hpNum.string = GameData.hero[id - 1].hp;
        this.attackNum.string = GameData.hero[id - 1].attack;


    },
    selectBtn(evt) {
        let _name = evt.currentTarget.name;
        cc.find("Canvas/game_start/rank").active = false;
        switch (_name) {
            case "start_btn":
                cc.find("Canvas/game_start/matching").active = true;
                break;
            case "switchbtn":/*切换人物选择界面*/
                cc.find("Canvas/game_start/select_hero").active = true;
                break;
        }
    },
    start() {

    },

    // update (dt) {},
});
